<?php
/**
 * 微信二维码管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/9 0009
 * Time: 15:33
 */
namespace application\admin\controller;
use application\admin\model\Excel;
use EasyWeChat\Foundation\Application;
use think\Config;
use think\Db;
use think\Session;
class QrcodeController extends  CommonController{
    public function index(){
        $where['app_type'] ='1';
        Session::get('user_id') != 1 ? $where['r.user_id'] = Session::get('user_id') : '';
        $lists = db("channel")->alias('r')->join('__WECHATAPP__ w','r.app_id = w.app_id','left')->where($where)->order('wechat_channel_id desc')->paginate(10);
        //print_r($lists);
        $this->assign('lists', $lists);
        return $this->fetch();
    }
    public function  info(){
        if(request()->isPost()){
            $id = $this->request->param('id');
            $map['reply_keywords'] = $this->request->param('reply_keywords');
            $map['channel_name'] = $this->request->param('channel_name');
            $map['app_id'] = $this->request->param('app_id');
            $result = Db::name('channel')->where('wechat_channel_id',$id)->update($map);
            if ($result){
                $this->success('操作成功', url('index'));
            }
        }

        $id = $this->request->param('id');
        $data = Db::name('channel')->where('wechat_channel_id',$id)->find();
        $where['app_type'] ='1';
        Session::get('user_id') != 1 ? $where['user_id'] = Session::get('user_id') : '';
        $app_name = Db::name('wechatapp')->where($where)->field('app_id,appname')->select();
        $this->assign('app_names',$app_name);
        $this->assign('info',$data);
        return $this->fetch();
    }
    public function add(){
        if(request()->isPost()){
            $id = $this->request->param('id');
            $map['reply_keywords'] = $this->request->param('reply_keywords');
            $channel_name = $this->request->param('channel_name');
            $map['app_id'] = $this->request->param('app_id');
            $map['user_id'] = Session::get('user_id');
            $data = Db::name('channel')->where($map)->find();
            if($data){
                return $this->error('添加失败 已有相同的关键词',url('index'));
            }else{
                $map['channel_name'] = $channel_name;
                $map['addtime'] = date('Y-m-d H:i:s',time());

                $result = Db::name('channel')->insert($map);
                if ($result){
                    $this->success('操作成功', url('index'));
                }
            }

        }
    }
    /*
     * 查看二维码
     */
    public function qrcode(){

        $id = $this->request->param('id');
        $str = "/uploads/images/code.jpg";
     //   $str = "<img src='/uploads/images/code.jpg' width='300px' height='300px' />";
        return $str;
        die();
        $data = Db::name('channel')->where('wechat_channel_id',$id)->find();
        $app_id = $data['app_id'];
        $keywords = $data['reply_keywords'];
        $app = Db::name('wechatapp')->where('app_id',$app_id)->find();
        $config = [
            'debug' => true,
            'app_id' => $app['appid'],  //wxa32e12e8fb8b21ca
            'secret' => $app['appsecret'],//20c211941b9db00a2e5cff0efa532d7b
            'token' => $app['token'],       //123456
            'aes_key' => '',                    // EncodingAESKey，安全模式与兼容模式下请一定要填写！！！
//            'log' => [
//                'level' => 'debug',
//                'permission' => 0777,
//                'file' => $app . '/runtime/easywechat.log',
//            ],
        ];
        $app = new Application($config);
        $qrcode = $app->qrcode;
        $result = $qrcode->forever($keywords);// 或者 $qrcode->forever("foo");
        $ticket = $result->ticket; // 或者 $result['ticket']
        $url = $qrcode->url($ticket);
        $content = file_get_contents($url); // 得到二进制图片内容
        $app_url = ROOT_PATH;
        file_put_contents($app_url . 'public/uploads/images/code.jpg', $content); // 写入文件
       // $app_url = Config::get('web_url');
     //   echo "<img src='/uploads/images/code.jpg' />";
    }
    /**
     * 导出表格
     */
    public function excel(){
        $id = $this->request->param('id');
        $list = Db::name('qrmember')->where('from_qr',$id)->select();

        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'id')
            ->setCellValue('B1', 'uid')
            ->setCellValue('C1', 's_time')
            ->setCellValue('D1', 'uns_time')
            ->setCellValue('E1', 'status')
            ->setCellValue('F1', 'nickname')
            ->setCellValue('G1','sex')
            ->setCellValue('H1','province')
            ->setCellValue('I1','city')
            ->setCellValue('J1','country')
            ->setCellValue('K1','tel')
            ->setCellValue('L1','address')
            ->setCellValue('M1','headimgurl')
            ->setCellValue('N1','from_qr')
            ->setCellValue('O1','subscribe_time');

        //设置F列水平居中
   //     $objPHPExcel->setActiveSheetIndex(0)->getStyle('P')->getAlignment()
         //   ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(15);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);

        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($list);$i++){

            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['id']);//添加ID
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['uid']);//添加姓名
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['s_time']);//添加年龄
            $objPHPExcel->getActiveSheet()->setCellValue('D'.($i+2),$list[$i]['uns_time']);//添加班级
            $objPHPExcel->getActiveSheet()->setCellValue('E'.($i+2),$list[$i]['status']);//添加电话
            $objPHPExcel->getActiveSheet()->setCellValue('F'.($i+2),$list[$i]['nickname']);//添加邮箱
            $objPHPExcel->getActiveSheet()->setCellValue('G'.($i+2),$list[$i]['sex']);//添加ID
            $objPHPExcel->getActiveSheet()->setCellValue('H'.($i+2),$list[$i]['province']);//添加姓名
            $objPHPExcel->getActiveSheet()->setCellValue('I'.($i+2),$list[$i]['city']);//添加年龄
            $objPHPExcel->getActiveSheet()->setCellValue('J'.($i+2),$list[$i]['country']);//添加班级
            $objPHPExcel->getActiveSheet()->setCellValue('K'.($i+2),$list[$i]['tel']);//添加电话
            $objPHPExcel->getActiveSheet()->setCellValue('L'.($i+2),$list[$i]['address']);//添加邮箱
            $objPHPExcel->getActiveSheet()->setCellValue('M'.($i+2),$list[$i]['headimgurl']);//添加班级
            $objPHPExcel->getActiveSheet()->setCellValue('N'.($i+2),$list[$i]['from_qr']);//添加电话
            $objPHPExcel->getActiveSheet()->setCellValue('O'.($i+2),$list[$i]['subscribe_time']);//添加邮箱

        }

        //7.设置保存的Excel表格名称
        $filename = '学生信息'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('学生信息');
        //9.设置浏览器窗口下载表格
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="'.$filename.'"');
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
        exit;;


    }
}